//---------------------------------------------------------------------
// File: ReceiveLocationVerifyStatusStep.cs
// 
// Summary: Gets the status of the receive port and stores it in context
//
// Copyright (c) Santosh Benjamin. All rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
// PURPOSE.
//---------------------------------------------------------------------

using System;
using System.Xml;
using BizUnit.BizUnitOM;
using Microsoft.BizTalk.ExplorerOM;

namespace BizUnit.Extensions.BizTalk
{
    using Enumerations;
    using Helpers;

    /// <summary>
    /// The ReceiveLocationConductorStep enables or disables a receive location.
    /// This step replaces the out of the box step with code that uses the OM 
    /// </summary>
    /// <remarks>
    /// The following shows an example of the Xml representation of this test step.
    /// 
    /// <code escaped="true">
    /// <TestStep assemblyPath="BizUnit.Extensions.BizTalk.dll" typeName="BizUnit.Extensions.BizTalk.ReceiveLocationConductorStep">
    ///		<DelayForCompletion>5</DelayForCompletion> <!-- Optional, seconds to delay for this step to complete -->
    ///		<ReceivePortName></ReceivePortName>
    ///		<ReceiveLocationName></ReceiveLocationName>
    ///		<Action>Enable</Action>
    /// </TestStep>
    /// </code>
    ///	
    ///	<list type="table">
    ///		<listheader>
    ///			<term>Tag</term>
    ///			<description>Description</description>
    ///		</listheader>
    ///		<item>
    ///			<term>DelayForCompletion</term>
    ///			<description>The number of seconds to deplay for this step to complete</description>
    ///		</item>
    ///		<item>
    ///			<term>ReceivePortName</term>
    ///			<description>The name of the receive port to containing the receive location to enable/dissable</description>
    ///		</item>
    ///		<item>
    ///			<term>ReceiveLocationName</term>
    ///			<description>The name of the receive location to enable/dissable</description>
    ///		</item>
    ///		<item>
    ///			<term>Action</term>
    ///			<description>Enable|Disable</description>
    ///		</item>
    ///	</list>
    ///	</remarks>

    public class ReceiveLocationVerifyStatusStep : ITestStep, ITestStepOM
    {
        private BtsCatalogExplorer root;
        private IBizTalkApplication appInstance;
        private ReceiveLocation rcvLocation;

        private string applicationName;
        private string connectionString;
        private string receivePortName;
        private string receiveLocationName;
        private PortStatus statusToVerify;

        public string ApplicationName
        {
            get
            {
                return applicationName;
            }
            set
            {
                applicationName = value;
            }
        }
        public string ReceivePortName
        {
            get
            {
                return receivePortName;
            }
            set
            {
                receivePortName = value;
            }
        }
        public string ReceiveLocationName
        {
            get
            {
                return receiveLocationName;
            }
            set
            {
                receiveLocationName = value;
            }
        }
        public string ConnectionString
        {
            get
            {
                return connectionString;
            }
            set
            {
                connectionString = value;
            }
        }

        public PortStatus StatusToVerify
        {
            get { return statusToVerify; }
            set { statusToVerify = value; }
        }


        #region ITestStep Members

        /// <summary>
        /// ITestStep.Execute() implementation
        /// </summary>
        /// <param name='testConfig'>The Xml fragment containing the configuration for this test step</param>
        /// <param name='context'>The context for the test, this holds state that is passed beteen tests</param>
        public void Execute(XmlNode testConfig, Context context)
        {
            ApplicationName = context.ReadConfigAsString(testConfig, "ApplicationName");
            ReceivePortName = context.ReadConfigAsString(testConfig, "ReceivePortName");
            ReceiveLocationName = context.ReadConfigAsString(testConfig, "ReceiveLocationName");
            ConnectionString = context.ReadConfigAsString(testConfig, "ConnectionString");
            StatusToVerify = (PortStatus)Enum.Parse(typeof(PortStatus), context.ReadConfigAsString(testConfig, "StatusToVerify"), true);

            XmlNode validationConfig = testConfig.SelectSingleNode("ValidationStep");

            Execute(context);

        }

        #endregion



        private void SetLocation()
        {
            root = BizTalkStepHelper.GetRoot(ConnectionString);
            appInstance = BizTalkStepHelper.GetApplication(ApplicationName, root);
            ReceivePort port = BizTalkStepHelper.GetReceivePort(appInstance, ReceivePortName);
            rcvLocation = BizTalkStepHelper.GetReceiveLocation(port, ReceiveLocationName);

        }
        #region ITestStepOM Members

        public void Execute(Context context)
        {
            Validate(context);
            SetLocation();
            PortStatus currStatus;
            if (rcvLocation.Enable)
            {
                currStatus = PortStatus.Enabled;
            }
            else
            {
                currStatus = PortStatus.Disabled;
            }


            if (StatusToVerify != currStatus)
            {
                throw new ApplicationException(string.Format("The receiveLocation: {0} was expected to be in Status {1}  but was in status {2} ", rcvLocation.Name, statusToVerify, currStatus));
            }
            else
            {
                context.LogInfo(string.Format("The recieve location: {0} was found to be in the required status {1} ", rcvLocation.Name, currStatus));
            }

        }

        public void Validate(Context context)
        {
            ArgumentValidation.CheckForEmptyString(ApplicationName, "ApplicationName");
            ArgumentValidation.CheckForEmptyString(ReceivePortName, "ReceivePortName");
            ArgumentValidation.CheckForEmptyString(ReceiveLocationName, "Receive Location Name");

        }

        #endregion
    }
}
